<template>
    <div class="forgetpassword">
         <div class="password-wrapper">
          <h2 class="password-title">忘记密码</h2>
          <div class="password-cont">
            <div class="password-box margin-b40">
              <img src="../../../../assets/img/icon_phone.png" class="iconimg">
              <yd-input slot="right" v-model="phone" regex="mobile" placeholder="请输入手机号码" class="phone"></yd-input>
            </div>
            <div class="password-box margin-b80">
               <img src="../../../../assets/img/icon_verification code.png" class="iconimg">
              <yd-input slot="right" v-model="code" placeholder="请输入验证码" class="code"></yd-input>
              <yd-sendcode slot="right" v-model="start" @click.native="sendCode" type="warning"></yd-sendcode>
            </div>
            <div class="password-btn" @click="next()">下一步</div>
            <div class="blank-btn" @click="blank()">返回登录</div>
          </div>
        </div>
    </div>
</template>

<script>
import apis from '../../../../../static/js/apis.js'
export default {
  components: {
    apis
  },
  data () {
    return {
      start: false,
      code: '',
      phone: ''
    }
  },
  methods: {
    sendCode () {
      if (this.phone !== '' && this.phone.length === 11) {
        let qs = require('querystring')
        this.$axios.post(apis + '/user/check',
          qs.stringify({
            username: this.phone
          })
        )
          .then(response => {
            console.log(response.data)
            if (response.data.code === 200) {
              console.log(response.data.message)
              this.$dialog.loading.open('发送中...')
              setTimeout(() => {
                this.start = true
                this.$dialog.loading.close()
                this.$dialog.toast({
                  mes: '已发送',
                  icon: 'success',
                  timeout: 1500
                })
              }, 1000)
            } else {
              this.$dialog.loading.open('发送中...')
              setTimeout(() => {
                this.start = true
                this.$dialog.loading.close()
                this.$dialog.toast({
                  mes: '获取验证码失败,请稍后再试！',
                  icon: 'error',
                  timeout: 1500
                })
              }, 1000)
            }
          })
          .catch(error => {
            console.log(error)
            this.$dialog.alert({mes: '手机号为空或不存在!'})
          })
      } else {
        this.$dialog.alert({mes: '请填写手机号!'})
      }
    },
    blank () {
      window.history.go(-1)
    },
    next () {
      if (this.phone === '' || this.code === '') {
        this.$dialog.alert({mes: '请填写正确信息!'})
      } else {
        let qs = require('querystring')
        this.$axios.post(apis + '/user/checkcode',
          qs.stringify({
            username: this.phone,
            code: this.code
          })
        )
          .then(response => {
            if (response.data.code === 200) {
              this.$router.push({
                path: '/Newpassword',
                query: {
                  phone: this.phone
                }
              })
            } else {
              this.$dialog.alert({mes: '验证码错误!'})
            }
          })
          .catch(error => {
            console.log(error)
          })
      }
    }
  }
}
</script>

<style scoped>
.forgetpassword{
position: fixed;
top:0;
bottom: 0;
width: 100%;
background: #fff;
}
.password-title{
  font-size: 24px;
  font-weight: bold;
  padding:0 40px;
  margin:145px 0;
}
.password-cont{
  overflow: hidden;
  padding: 0 40px;
}
.password-box{
  width: 100%;
  display: flex;
  height: 40px;
  border-bottom: 1px solid #c4c4c4;
}
.phone{
flex:1;
font-size: 14px;
}
.iconimg{
  width: 15px;
  height: 20px;
  margin-right: 10px;
  margin-top: 10px;
}
.code{
  flex:1;
  font-size: 14px;
}
.margin-b40{
  margin-bottom: 40px;
}
.margin-b80{
  margin-bottom: 80px;
}
.password-btn{
  width: 100%;
  border-radius: 10px;
  background: #ffd602;
  color:#fff;
  font-size: 15px;
  font-weight: bold;
  height: 40px;
  line-height: 40px;
  text-align: center;
  margin-bottom: 25px;
}
.blank-btn{
   width: 100%;
   color:#ffd602;
   font-size: 12px;
   text-align: center;
}
</style>
